<template>
  <div>
    <van-picker :default-index="user.gender" show-toolbar :columns="columns" @confirm='onConfirm' @cancel="$emit('close')" @change="onChange" />
  </div>
</template>
<script>
import { updateProfile } from '@/api/user.js'
export default {
  props: {
    gender: { type: Object, required: true }
  },
  data() {
    return {
      columns: ['男', '女'],
      user: this.gender
    }
  },
  methods: {
    onChange(picker, value, index) {
      this.user.gender = index
    },
    async onConfirm() {
      this.$toast.loading({ message: '发布中...', forbidClick: true })

      const { data: res } = await updateProfile(this.$store.state.user.id, this.user)
      if (res.code === 200) {
        this.$store.commit('setProfile', this.user)
        this.$toast.success('修改成功')
        this.$emit('close')
      }
    }
  }
}
</script>
